<template>
  <div class="login">
    <!-- 顶部导航 -->
    <div class="top">
      <van-nav-bar>
        <template #left>
          <i class="iconfont top_icon iconbtn_nav_back"></i>
        </template>
      </van-nav-bar>
    </div>

    <!-- 表单 -->
    <div class="contenr">
      <h4 class="bd">您好，请登录</h4>
      <van-form ref="form" name="myForm">
        <van-cell-group>
          <van-field
            :rules="rules.mobile"
            v-model="form.mobile"
            name="mobile"
            placeholder="请输入手机号"
          >
            <template #left-icon>
              <i class="iconfont myicon iconbianzu1"></i>
            </template>
          </van-field>
          <van-field
            :rules="rules.code"
            v-model="form.code"
            placeholder="请输入验证码"
          >
            <template #left-icon>
              <i class="iconfont myicon iconyanzhengma"></i>
            </template>
            <template #button>
              <span class="col" @click="getCode">获取验证码</span>
            </template>
          </van-field>
        </van-cell-group>
        <p>
          登录即同意<span class="col">《用户使用协议》</span>和<span class="col"
            >《隐私协议》</span
          >
        </p>
        <van-button size="large" round type="danger" @click="onsubtim"
          >确定</van-button
        >
      </van-form>
    </div>
  </div>
</template>
<script>
import { getcodeApi, getLoginApi } from '../../api/login'
export default {
  name: 'login',
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'onBlur' },
          {
            validator: value => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                value
              )
            },
            message: '非法数据,请输入正确的手机号',
            trigger: 'onBlur'
          }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'onBlur' },
          {
            pattern: /^\d{4}$/,
            message: '验证码长度为4',
            trigger: 'onBlur'
          }
        ]
      }
    }
  },
  methods: {
    getCode () {
      // 进行局部验证
      // 验证成功发起请求
      this.$toast.loading('数据请求中...')
      this.$refs.form.validate('mobile').then(res => {
        getcodeApi({
          mobile: this.form.mobile
        }).then(res => {
          //   console.log(res)
          // 判断是否请求成功
          if (res.data.code === 200) {
            // 请求成功返回code值
            this.$toast.success(res.data.data)
          } else {
            this.$toast.fail(res.data.message)
          }
        })
      })
    },
    onsubtim () {
      // 全局校验
      // 校验成功发起请求
      this.$refs.form
        .validate()
        .then(() => {
          getLoginApi({
            ...this.form
          }).then(res => {
            console.log(res)
            if (res.data.code === 200) {
              this.$toast.success(res.data.message)
              //   跳转页面
            } else {
              // 如果请求错误 刷新验证码
              this.$toast.fail(res.data.message)

              this.getCode()
            }
          })
        })
        .catch(() => {})
    }
  }
}
</script>
<style lang="less" scoped>
.login {
  .top_icon {
    font-size: 44px;
  }
  /deep/ .van-nav-bar__left {
    padding-left: 0px;
  }
  .contenr h4 {
    margin: 50px 0 50px 15px;
  }
  .myicon {
    padding-left: 35px;
    padding-right: 21px;
  }
  p {
    margin: 15px 0 44px 15px;
    font-size: 12px;
    color: #181a39;
  }
  span.col {
    color: #00b8d4;
  }
  .van-button {
    margin-left: 15px;
    margin-right: 15px;
    width: 345px;
    height: 44px;
    background: #e40137;
  }
}
</style>
